package com.lcredu.web;/**
 * @author 黎长容
 * @date 2025-07-09 12:38
 */

import com.lcredu.entity.Cart;
import com.lcredu.entity.Member;
import com.lcredu.entity.Order;
import com.lcredu.entity.OrderItem;
import com.lcredu.service.OrderService;
import com.lcredu.service.impl.OrderServiceImpl;
import com.lcredu.utils.JDBCUtilsByDruid;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import javax.xml.transform.Source;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "OrderServlet", urlPatterns = "/OrderServlet")
public class OrderServlet extends BasicServlet {
    private OrderService orderService = new OrderServiceImpl();

    /**
     * 查看订单详情
     * 展示那个订单里面的订单项
     */
    protected void checkOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("orderId");
        //取出订单
        Order order = orderService.getOrderById(id);
        BigDecimal totalPrice = order.getPrice();

        //在这个地方做事务测试
        List<OrderItem> orderItems = orderService.getOrderItems(id);
        int totalSize = orderItems.size();

        request.getSession().setAttribute("orderItems", orderItems);
        request.setAttribute("totalPrice", totalPrice);
        request.setAttribute("totalSize", totalSize);
        request.getRequestDispatcher("views/order/order_detail.jsp").forward(request, response);
    }

    /**
     * 查看订单
     */
    protected void checkOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String orderId = request.getParameter("orderId");
        Order order = orderService.getOrderById(orderId);
        System.out.println("order=" + order);
        if (order == null) {
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }
        request.setAttribute("order", order);
        request.getRequestDispatcher("views/order/order.jsp").forward(request, response);
    }


//    protected void saveOrder2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("调用了saveOrder2!!!");
//        //业务代码
//        Cart cart = (Cart) req.getSession().getAttribute("cart");
//
//        //如果cart == null , 说明会员没有购买任何家居, 转发到首页
//        //这里我们需要补充一个逻辑, 购物车在sessoin, 但是没有家居数据
//        if (null == cart ) {
//            req.getRequestDispatcher("/index.jsp").forward(req, resp);
//            return;
//        }
//
//        //获取当前登录的memberId
//        Member member = (Member) req.getSession().getAttribute("user");
//        //说明用户没有登录, 转发到登录页面
//        if (null == member) {
//
//            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
//            return;//直接返回
//        }
//        //就可以生成订单
//        //老韩分析
//        //1. 如果我们只是希望对orderService.saveOrder()进行事务控制
//        //2. 我们可以不使用过滤器, 我们直接在这个位置，进行提交和回滚即可
//        //String orderId = null;
//        //try {
//        //    orderId = orderService.saveOrder(cart, member.getId());
//        //    JDBCUtilsByDruid.commit();//提交
//        //} catch (Exception e) {
//        //    JDBCUtilsByDruid.rollback();
//        //    e.printStackTrace();
//        //}
//        String orderId = orderService.saveOrder(cart, member.getId());
//        req.getSession().setAttribute("orderId", orderId);
//
//        //使用重定向方式到 checkout.jsp
//        resp.sendRedirect(req.getContextPath() + "/views/order/checkout.jsp");
//    }

    protected void saveOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("ok...");
        //todo 业务代码
        //完成订单生成
        Member user = (Member) request.getSession().getAttribute("user");
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        if (user == null) {
            //没有登陆
            request.getRequestDispatcher("views/member/login.jsp").forward(request, response);
            return;
        }
        //如果cart里面的购物车被清空了就会产生空订单 
        //加一个判断
        if (cart == null || cart.isEmpty()) {
//            request.getRequestDispatcher("views/member/login.jsp").forward(request,response);
            response.sendRedirect(request.getContextPath() + "/index.jsp");
            return;
        }

//        String orderId = null;
//        try {
//            orderId = orderService.saveOrder(cart, user.getId());
//            //如果没有问题就提交事务
//            JDBCUtilsByDruid.commit();
//        } catch (Exception e) {
//            //如果有问题就回滚
//            JDBCUtilsByDruid.rollback();
//            throw new RuntimeException(e);
//        }

        String orderId = orderService.saveOrder(cart, user.getId());

        request.getSession().setAttribute("orderId", orderId);


        response.sendRedirect(request.getContextPath() + "/views/order/checkout.jsp");
    }

    protected void sayHi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Hi---hello");
    }
}